package com.example.checkqufuface.Customer;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.baselibrary.Utils.Customer;
import com.example.baselibrary.Utils.SPUtils;
import com.example.baselibrary.Utils.StringUtil;
import com.example.checkqufuface.Adapter.SexAdapter;
import com.example.checkqufuface.Adapter.TypeAdapter;
import com.example.checkqufuface.R;
import com.example.checkqufuface.bean.QufufindCertificateTypeResultBean;

import java.util.ArrayList;
import java.util.List;

public class CustomImformationWindow extends PopupWindow {
    private static final String TAG = "CustomPopWindow";
    private final View view;
    private Activity context;
    private BaseQuickAdapter<String, BaseViewHolder> adapter;
    private QuerenTvListener querenTvListener;
    private QuXiaoTvListener quxiaoTvListener;
    private List<QufufindCertificateTypeResultBean.DataBean> typeData;

    private String typeId;

    private String name;
    private String sex;
    private String sexType;
    private String id;


    public CustomImformationWindow(Activity context, List<QufufindCertificateTypeResultBean.DataBean> typeDate,String name,String sex,String id) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.widget_imformation, null);//alt+ctrl+f
        this.context = context;
        this.typeData = typeDate;
        this.name = name;
        this.sex = sex;
        this.id = id;
        initView();
        initPopWindow();
    }


    private void initView() {

        final List<String> SexList = new ArrayList<>();
        SexList.add("男");
        SexList.add("女");
        // 获取 绑定 ID
        TextView cancelTv = view.findViewById(R.id.tv_wg_qu_xiao_infor);
        TextView querenTv = view.findViewById(R.id.popup_queren_infor);
        final EditText etName = view.findViewById(R.id.tv_widget_name);
        final TextView tvSex = view.findViewById(R.id.tv_widget_xinbie);
        final TextView tvType = view.findViewById(R.id.tv_widget_leixin);
        final EditText etNumber = view.findViewById(R.id.tv_widget_haoma);
        final EditText etPhone = view.findViewById(R.id.tv_widget_shouji);

        //一进来 就要 先对这些数据赋值  如果不为空的话
        etName.setText((String) SPUtils.get(context, Customer.QUFU_IMFOR_NAME, ""));
        tvSex.setText((String) SPUtils.get(context, Customer.QUFU_IMFOR_SXE, ""));
        tvType.setText((String) SPUtils.get(context, Customer.QUFU_IMFOR_TYPE, ""));
        etNumber.setText((String) SPUtils.get(context, Customer.QUFU_IMFOR_ID, ""));

        if (!StringUtil.isNullOrEmpty(id)) {
            etName.setText(name);
            tvSex.setText(sex);
            tvType.setText("身份证");
            etNumber.setText(id);
        }


        etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                //输入完毕后 ，保存数据
                SPUtils.put(context, Customer.QUFU_IMFOR_NAME, s);
            }
        });

        etNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                //输入完毕后 ，保存数据
                SPUtils.put(context, Customer.QUFU_IMFOR_ID, s);
            }
        });

        //两个recycleview
        RecyclerView rvSex = view.findViewById(R.id.rv_sex_show);
        SexAdapter sexAdapter = new SexAdapter(context, SexList);
        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(context);
        rvSex.setLayoutManager(linearLayoutManager1);
        rvSex.setAdapter(sexAdapter);
        sexAdapter.SetSexClickListener(new SexAdapter.SexClickListener() {
            @Override
            public void sexSelect(int pos) {
                tvSex.setText(SexList.get(pos));
                sex = SexList.get(pos);
                SPUtils.put(context, Customer.QUFU_IMFOR_SXE, SexList.get(pos));
            }
        });
        //类型 类型 类型
        RecyclerView rvType = view.findViewById(R.id.rv__widget_leixin);
        TypeAdapter typeAdapter = new TypeAdapter(context, typeData);
        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(context);
        rvType.setLayoutManager(linearLayoutManager2);
        rvType.setAdapter(typeAdapter);

        typeAdapter.SetTypeClickListener(new TypeAdapter.TypeClickListener() {
            @Override
            public void typeSelect(int pos) {
                tvType.setText(typeData.get(pos).getCertificateTypeName());
                typeId = typeData.get(pos).getCertificateTypeId()+ "";
                SPUtils.put(context, Customer.QUFU_IMFOR_TYPE, typeData.get(pos).getCertificateTypeName());
                if (!typeData.get(pos).getCertificateTypeName().equals("身份证")) {
                    etNumber.setText("");
                }
            }
        });

        querenTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtil.isNullOrEmpty(typeId)) {
                    for (int i = 0; i < typeData.size(); i++) {
                        if (typeData.get(i).getCertificateTypeName().equals("身份证")){
                            typeId = typeData.get(i).getCertificateTypeId() + "";
                        }
                    }
                }
                if (sex.equals("男")) {
                    sexType = "0";
                }else {
                    sexType = "1";
                }
                querenTvListener.querenTv(etName.getText().toString(),sexType,typeId,etNumber.getText().toString(),etPhone.getText().toString());
            }
        });


        cancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    private void initPopWindow() {
        this.setContentView(view);
        // 设置弹出窗体的宽
        this.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        // 设置弹出窗体的高
        this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        // 设置弹出窗体可点击()
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.mypopwindow_anim_style);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x000000);
//        //设置弹出窗体的背景
        this.setBackgroundDrawable(dw);
//        backgroundAlpha(context, 0.5f);//0.0-1.0
        this.backgroundAlpha(context, 0.5f);
    }

    /**
     * 设置添加屏幕的背景透明度(值越大,透明度越高)
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }

    public interface QuerenTvListener {
        void querenTv(String name,String sex,String type,String number,String phone);
    }

    public void setQuerenTvListener(QuerenTvListener querenTvListener) {
        this.querenTvListener = querenTvListener;
    }

    public interface QuXiaoTvListener {
        void quXiaoTv();
    }

    public void setQuXiaoTvListener(QuXiaoTvListener querenTvListener) {
        this.quxiaoTvListener = querenTvListener;
    }
}
